<?php

/***  DOCUMENTATION LAYER

Ceo Framework File System Class

Name: CeoFso
Last Update: Sep 2007
Author: Tom at klenwell@gmail.com

DESCRIPTION
  File handler

METHODS
  MAGIC
  CeoFso($debug=0, $oid=null)    *php 4 constructor*
  __construct($debug, $oid)          *php 5 constructor*
  __destruct()  
  
  PUBLIC
  print_d($message, $color='c33')
  print_r()
  dump()
  
  PRIVATE
  _set_filename()
  _set_dirpath()
  
USAGE
  $CeoFso = new CeoFso();


NOTES

______________________________________________________________________________*/

// Load File of Base Class (n/a)


// CeoFso
/*____________________________________________________________________________*/
class CeoFso
{
/* PUBLIC PROPERTIES */
var $debug = 0;
var $class_name = __CLASS__;
var $oid = '';
var $DS = DIRECTORY_SEPARATOR;

/* PRIVATE PROPERTIES */
var $_filename = '';
var $_dirpath = '';


/* ** MAGIC METHODS ** */
// php4 constructor
function CeoFso($debug=0, $oid=null)
{
  $this->__construct($debug, $oid);
  register_shutdown_function( array($this, '__destruct') );
}
// END constructor

// php5 constructor
function __construct($debug=0, $oid=null)
{
  // default
  $this->debug = $debug;
  $this->oid = ( empty($oid) ) ? $this->class_name : $oid;
  $this->_set_filename();
  $this->_set_dirpath();
  
  // additional code 
  
  // debug
  if ( $this->debug ) $this->print_d('debugging is active for oid ' . $this->oid);
  if ( $this->debug ) $this->print_d('constructor complete for class ' . __CLASS__);
}
// END constructor

// destructor
function __destruct()
{
  if ( $this->debug ) $this->print_d("destroying class {$this->class_name} (oid: {$this->oid})");
}
// END destructor



/* ** PUBLIC METHODS ** */
// method: read file
function read($fpath, $mode='rb')
{
  $content = '';    // return

  // file_get_contents (php >= 4.3, faster)
  if ( function_exists('file_get_contents') ) return file_get_contents($fpath);
  
  // get handle (open file)
  if ( !$handle = $this->_get_handle($fpath, $mode) ) return 0;
  
  // read contents
  // see notes: http://www.php.net/manual/en/function.fgets.php#68144, #77252
  #while ( !feof($handle) ) $content .=  fscanf($handle, "%s\n");
  while ( !feof($handle) ) $content .= fgets($handle, 4096);
  
  fclose($handle);
  return $content;
}

// method: append file
function append($fpath, $content)
{
  $is_appended = 0;
  if ( $this->write_file($content, $fpath, $overwrite=0) ) $is_appended = 1;
  return $is_appended;  
}

// method: overwrite file
function overwrite($fpath, $content)
{
  $is_written = 0;
  if ( $this->write_file($fpath, $content, $overwrite=1) ) $is_written = 1;
  return $is_written;  
}

// method: write to file
function write_file($fpath, $content, $overwrite=1)
{
  $is_written = 0;
  $_MODE = array( 'overwrite' => 'w+', 'append' => 'a+', );

  // set mode
  $mode = ( $overwrite ) ? $_MODE['overwrite'] : $_MODE['append'] ;

  // get handle (open file)
  if ( !$handle = $this->_get_handle($fpath, $mode) ) return 0;
  
  // write
  if ( fwrite($handle, $content) === FALSE )
  {
    trigger_error("unable to write to file [$fpath]", E_USER_WARNING);
    $is_written = 0;
  }
  else
  {
    $is_written = 1;
  }    
    
  fclose($handle);
  return $is_written;
}
// END method

// method: normalize slashes in a path or url and remove double slash endings
function normalize_slashes($path, $slash='/')
{
  $new_path = '';
  $new_path = str_replace('/', $slash, $path);
  $new_path = str_replace('\\', $slash, $new_path);
  return $new_path;
}
// END method


// method: print_d
function print_d($message, $color='#c33')
{
  $out = "<div style='line-height:1.5em; font-family:monospace; color:$color;'>$message</div>";
  echo $out;
  return;
}
// END method

// method: print_r
function print_r($Mixed)
{
  $return = htmlspecialchars(print_r($Mixed, 1));
  $return = "<pre>$return</pre>";
  return $return;
}
// END method

// method: dump
function dump()
{
  echo $this->print_r($this);
  return;
}
// END method



/* ** PRIVATE METHODS ** */
function _get_handle($fpath, $mode) 
{
  $handle = 0;
  
  // open file (with lock?)
  if ( !$handle = fopen($fpath, $mode) )
  {
    trigger_error("unable to open file for writing [$fpath]", E_USER_WARNING);
    return 0;
  }
  
  return $handle;
}

function _set_filename() { $this->_filename = basename(__FILE__); }
function _set_dirpath() { $this->_dirpath = dirname(__FILE__) . $this->DS; }

} // end class
/*____________________________________________________________________________*/

?>
